# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
import time
import re
import csv
import os
from pymongo import MongoClient
from utils.HttpUtils import HttpUtils


requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告

"""
(.*?): (.*)
"$1":"$2",

http://www.bepurestandards.com/
1.中文名称中含有“混标”
2.产品详细信息中-产品详情含有多组数据“混标信息”
以上似为混标数据
"""


class TestRequest():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        # requests.utils.dict_from_cookiejar(html.cookies)  # 获取cookies转dict
        # res.cookies.get_dict()  # 获取cookies
        self.cookie = cookiejar.CookieJar()
        # self.ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        # self.ua = UserAgent(verify_ssl=False)  #
        self.headers = {
            'Accept': 'application/json, text/javascript, */*; q=0.01',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Connection': 'keep-alive',
            'Cookie': 'ASP.NET_SessionId=djv044iws51surt1eck5l0uw; Hm_lvt_50fd634314f991c065b70906f34a67b3=1629507751; UM_distinctid=17b663b165817-0cff2ec9b8026d-c343365-144000-17b663b16594c3; Hm_lpvt_50fd634314f991c065b70906f34a67b3=1629507756; CNZZDATA1278877426=208186265-1629507735-%7C1629507738',
            'DNT': '1',
            'Host': 'www.bepurestandards.com',
            'Referer': 'http://www.bepurestandards.com/list/%E5%86%9C%E8%8D%AF%E6%A3%80%E6%B5%8B%E6%A0%87%E5%87%86%E7%89%A9%E8%B4%A8/list/',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36',
            'X-Requested-With': 'XMLHttpRequest'
        }


    def get_contents(self, cursor, type_class):
        postData = {
            'oper': 'Product_list',
            'time': '0.8803539449513966',
            'title': type_class,
            'page': cursor,
            'oderby': '品牌',
            'sort': '降序',
            'brand': 'BePure'
        }
        url = f"http://www.bepurestandards.com/a.aspx"
        html = HttpUtils.do_request("GET", url, self.headers, postData)

        data_json = json.loads(html.text)
        pageCount = data_json['table1'][0]['pagecount']
        dict_data = dict()
        for d in data_json['table2']:
            dict_data['分类'] = type_class
            dict_data['产品编号'] = d['code']
            dict_data['中文名称'] = d['name']
            dict_data['英文名称'] = d['name2']
            dict_data['纯度'] = d['purity']
            dict_data['品牌'] = d['brand']
            dict_data['包装'] = d['pack']
            dict_data['库存'] = d['cnum']
            dict_data['有效期'] = d['enddate']
            dict_data['价格'] = d['price']
            id = d['id']
            url_child = f'http://www.bepurestandards.com/show/{id}/{type_class}/Y/true'

            # 获取温度
            wd_dict = self.get_wd(id, dict_data)
            # 产品详情列表
            self.get_detail(id, wd_dict)

    # 获取温度
    def get_wd(self, id, item):
        post_data_child = {
            'oper': 'Detailed_product',
            'shopID': id,
            'sale': 'Y'
        }
        html_child = HttpUtils.do_request("GET", "http://bepurestandards.com/a.aspx", self.headers, post_data_child)
        json_child = json.loads(html_child.text)
        item['保存温度'] = json_child['info'][0]['temp']

        return item

    # 产品详情列表
    def get_detail(self, id, item):
        post_data_child = {
            'oper': 'InvertoryTest',
            'shopID': id
        }
        html_child = HttpUtils.do_request("GET", "http://bepurestandards.com/a.aspx", self.headers, post_data_child)
        json_child = json.loads(html_child.text)
        total = int(json_child['total'])  # 总数据
        if total > 0:
            for t in json_child['table']:
                dict_data_d = dict()
                dict_data_d['序号'] = t['no']
                dict_data_d['中文'] = t['中文']
                dict_data_d['CAS_1'] = t['cas'] + "\t"
                dict_data_d['描述'] = t['描述']
                dict_data_d['浓度'] = t['浓度']
                dict_data_d['溶剂'] = t['溶剂']
                # 合并基础数据与产品详情列表
                dict_data_all = item | dict_data_d
                # # 保存数据
                HttpUtils.dict_to_csv("振翔_混标", dict_data_all)
        elif item['中文名称'].find('混标') >= 0:
            # # 保存数据
            HttpUtils.dict_to_csv("振翔_混标", item)
        else:
            # 保存数据
            HttpUtils.dict_to_csv("振翔", item)


if __name__ == '__main__':
    # conn = MongoClient('127.0.0.1', 27017)
    # db = conn["Test"]
    test = TestRequest()

    for i in range(1, 56):
        print(f"============第【{i}】页=============")
        test.get_contents(i, "农药检测标准物质")